/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 15:07:52
 * describe:登录
 */
APP.controller('LoginController', ['$scope','$rootScope', 'LoginService', '$state','$localstorage','UserService',
    function ($scope,$rootScope, LoginService, $state,$localstorage,UserService) {

        /***变量声明***/
        $scope.user = {
            username:'',
            password:''
        };

        /**
         * 初始化函数
         */
        $scope.init = function () {

        };

        /**
         * 登录方法
         */
        $scope.login = function () {
            LoginService.doLogin($scope.user.username, $scope.user.password)
                .success(function (response) {
                    console.log('成功回调方法', response);
                    UserService.setUser(response.data);
                    $rootScope.token = response.data.token;
                    // $localstorage.setObject(USER_TOKEN, $rootScope.token);
                    window.localStorage.setItem('user_token', $rootScope.token);
                    // console.log('$rootScope.token', window.localStorage.setItem('USER_TOKEN'));
                    $state.go('main');
                }).error(function () {
                
            })
        };


        /*
         $scope.init();//如果页面缓存则用此方法初始化
         */
        $scope.$on('$ionicView.beforeEnter', function () {
            $scope.init();
        });
    }
]);

/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 15:07:52
 * describe:登录
 */
APP.service('LoginService', ['$http', 'UrlService', function ($http, UrlService) {
    /**
     * 登录方法
     * @param username  登录用户名
     * @param password  登录密码
     */
    this.doLogin = function (username, password) {
        var param = {
            username: username,
            password: password
        };
        return $http.post(UrlService.getUrl('LOGIN'), param);
    }

}]);
